import { message } from 'antd';
import dayjs from 'dayjs';
export const test = 'test';
import Decimal from 'decimal.js';

export const wait = (n = 500) => new Promise((resolve) => setTimeout(resolve, n));

// 检测接口是否成功
export const isSuccess = (res: any) => {
  if (res?.code !== 10000) {
    message.error(res?.msg);
    return false;
  }
  return true;
};

// 检测是否是localhost
export const isLocal = () => window.location.hostname === 'localhost';

// 数组对象转枚举
export const arrayToEnum = (arr: any[]) => {
  const obj: any = {};
  arr.forEach((item, index) => {
    obj[item?.key] = item?.value;
  });
  return obj;
};

// 生效方式
export const tifTypes: any = {
  gtc: '普通委托',
  ioc: '立即成交或取消',
  poc: '被动委托',
  fok: '完全成交或取消',
};

// 订单结束方式
export const orderStatusTypes: any = {
  filled: '完全成交',
  cancelled: '已撤销',
  liquidated: '强平撤销',
  ioc: '未立即完全成交',
  auto_deleveraged: '自动减仓撤销',
  reduce_only: '增持仓位撤销，因为设置reduce_only或平仓',
};

// 来源
export const sourceTypes = (text: string) => {
  if (text.includes('web')) return '网页';
  if (text.includes('app')) return '移动端';
  if (text.includes('liq')) return '强制平仓';
  if (text.includes('ao')) return '止盈/止损';
  if (text.includes('close_all')) return '一键平仓';
  return text;
};

// 将时间转化为时间戳
export const getTimestamp = (time: string) =>
  parseInt((new Date(time).getTime() / 1000).toString(), 10);

// 格式化数字，添加千分位,数字，保留位数
export function formatNum(num: any, fixed = 2) {
  const _num = Number(num).toFixed(fixed);
  const numStr = _num.toString();
  const [integerPart, decimalPart] = numStr.split('.');
  const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  return decimalPart ? `${formattedInteger}.${decimalPart}` : formattedInteger;
}

// 保留小数不四舍五入
export function numNoSiWu(num: any, n: number = 2) {
  const multiplier = Math.pow(10, n);
  return Math.floor(num * multiplier) / multiplier;
}

// 将枚举转换为数组
export const enumToArray = (data: any) =>
  Object.entries(data).map(([key, value]) => ({ name: value, value: key }));

// 根据特殊字符获取员工姓名
export const getEmployeeName = (employees: any[], text: string) => {
  const id: any = text?.split('-')[2];
  const name = employees[id] || sourceTypes(text);
  return name || '';
};

// 列表计算usdt数量,size仓位的size有变化
// 列表计算usdt数量,size仓位的size有变化
export const getUsdtNum = (record: any, size = null) => {
  const newSize = size || record?.size;
  let res = countUsdtNum(record?.price, newSize, record?.contractInfo?.quanto_multiplier);
  return +res;
};

// 计算usdt数量
export const countUsdtNum = (price: any, size: any, chengShu: any) => {
  let res = (price * size * chengShu).toFixed(1);
  return res;
};

// 检测是否为NaN
export const getSumOrderNum = (value: any) => {
  if (!value || Number.isNaN(value)) {
    return '--';
  } else {
    return value.toFixed(2);
  }
};

// 全仓逐仓信息
export const cangWeiTypes: any = { all: '全仓', zhu: '逐仓' };

// 设置默认员工
export const setDefEmployeeId = (yuanGongs: any[]) =>
  !isLocal() ? Object.keys(yuanGongs)[0] : '5';

// 处理订单、仓位列表方向，根据用户方向
export const handleDirection = (res: any, list: any, direction: any) => {
  if (direction === 1) return res;
  return list.filter((item: any) => item !== res)[0];
};

// 获取最大时间
export const getBigTime = (t1: any, t2: any) => {
  if (t1 > t2) return t1;
  return t2;
};

// 使用big处理数字
export const jingDuNum = (num: number | string) => new Decimal(num);

// 加法
export function jiaFa(list: any[]) {
  if (!Array.isArray(list)) return 0;
  let sum = jingDuNum(0);
  for (let item of list) {
    sum = sum.add(jingDuNum(item));
  }
  return +sum;
}

// 乘法
export function chengFa(list: any[]) {
  if (!Array.isArray(list)) return 0;
  let res = jingDuNum(1);
  for (let item of list) {
    res = res.times(jingDuNum(item));
  }
  return +res;
}

// 计算usdt数量
export const countUsdtNumNew = (price: any, size: any, chengShu: any) => {
  let res: any = chengFa([price, size, chengShu]).toFixed(1);
  return res;
};
